package com.fulihui.wgj.consumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.util.Encode64Utils;
import com.fulihui.open.shortlink.model.ShortUrl;
import com.fulihui.open.shortlink.service.ShortUrlService;
import com.fulihui.wgj.consumer.service.ShortLinkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: LiuJie
 * @version: 2018/7/19 10:23
 * @description:
 */
@Service
@Slf4j
public class ShortLinkServiceImpl  implements ShortLinkService{

    /**
     * 测试环境下 group = "default"
     * todo 生产环境下 可能要去掉0
     */
    @Reference(version = "1.0.0",group = "*")
    private ShortUrlService shortUrlService;

    @Override
    public ShortUrl queryUrl(String openid, String appid, String viewUrl) {
        if(StringUtils.isBlank(viewUrl)){
            log.info("viewUrl is blank");
            return null;
        }
        String urlId = StringUtils.substringAfterLast(viewUrl, "/");
        String id = Encode64Utils.UnDecodeString(urlId);
        if (StringUtils.isBlank(id)) {
            return null;
        }

        ShortUrl shortUrl = new ShortUrl();
        shortUrl.setId(Long.valueOf(id));
        List<ShortUrl> shortUrls = shortUrlService.getByShortUrl(shortUrl);
        if (CollectionUtils.isEmpty(shortUrls)) {
            log.info("short url is not found.");
            return null;
        }

        return shortUrls.get(0);

    }
}
